import React, { useState } from 'react';
import axiosInstance from '../api/axiosInstance';
import { Button, TextField, Table, TableBody, TableCell, TableHead, TableRow, Paper } from '@mui/material';

function WorkloadPrediction() {
  const [year, setYear] = useState('');
  const [weekNumber, setWeekNumber] = useState('');
  const [predictions, setPredictions] = useState([]);

  const fetchPredictions = async () => {
    try {
      const response = await axiosInstance.post('api/predict_workload/', {
        year: parseInt(year, 10),
        week_number: parseInt(weekNumber, 10)
      });
      console.log(response)
      setPredictions(response.data);
    } catch (error) {
      console.error('Ошибка при прогнозировании рабочей нагрузки:', error);
    }
  };

  return (
    <Paper>
      <form
      onSubmit={(e) => { e.preventDefault(); fetchPredictions(); }}>
        <TextField
          label="Год"
          type="number"
          value={year}
          onChange={(e) => setYear(e.target.value)}
          required
        />
        <TextField
          label="Номер недели"
          type="number"
          value={weekNumber}
          onChange={(e) => setWeekNumber(e.target.value)}
          required
        />
        <Button type="submit" variant="contained" color="primary"  style={{ margin: '8px' }}>Прогнозировать</Button>
      </form>
      <Table>
        <TableHead>
          <TableRow>
            <TableCell>Модальность</TableCell>
            <TableCell>Прогнозируемое количество исследований</TableCell>
            <TableCell>Прогнозируемое количество УЕ</TableCell>
          </TableRow>
        </TableHead>
        <TableBody>
          {predictions.map((prediction, index) => (
            <TableRow key={index}>
              <TableCell>{prediction.modality}</TableCell>
              <TableCell>{prediction.predicted_quantity}</TableCell>
              <TableCell>{prediction.predicted_ue}</TableCell>
            </TableRow>
          ))}
        </TableBody>
      </Table>
    </Paper>
  );
}

export default WorkloadPrediction;